package com.online.controller;

import com.alibaba.fastjson.JSONObject;
import com.online.entity.*;
import com.online.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping(value = "/admin")
public class AdminController {

    @Autowired
    BlankService blankService;

    @Autowired
    AdminService adminService;

    @Autowired
    TeacherService teacherService;

    @Autowired
    UserService userService;

    @Autowired
    OptionService optionService;

    @Autowired
    AnswerService answerService;

    /**
     * 显示所有的教师信息
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/showAllTeacherInfo", method = RequestMethod.GET, produces = "text/html;charset=UTF-8")
    public String findAllTeacher(HttpServletRequest request) {

        List<Teacher> teacherList = teacherService.showAllTeacherInfo();

        if (!teacherList.isEmpty()) {
            request.setAttribute("teacherList", teacherList);
            return "admin/teacher_list";
        } else {
            request.setAttribute("result", "暂无教师信息");
            return "admin/teacher_list";
        }
    }

    /**
     * 禁用或者解冻教师
     * @param teacherId
     * @param status
     * @param workNumber
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/updateTeacherStatus", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
    public String updateTeacherStatus(@RequestParam(value = "teacherId", required = false) Integer teacherId,
                                      @RequestParam(value = "status", required = false) Integer status,
                                      @RequestParam(value = "workNumber",required = false) String workNumber) {

        //更新教师的状态
        int result = teacherService.updateTeacherStatus(teacherId, status);

        if(result != 0) {
            result = userService.updateUserStatus(workNumber,status);
        }

        if (result != 0) {
            CorrectJsonObject correctJsonObject = new CorrectJsonObject("操作成功");
            return JSONObject.toJSONString(correctJsonObject);
        } else {
            ErrorJsonObject errorJsonObject = new ErrorJsonObject(101,"操作失败");
            return JSONObject.toJSONString(errorJsonObject);
        }
    }

    /**
     * 插入选择题的信息
     * @param optionQuestion
     * @param optionA
     * @param optionB
     * @param optionC
     * @param optionD
     * @param knowledge
     * @param answerContent
     * @param answerType
     * @param answerKeys
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/insertOption",method = RequestMethod.POST,produces = "text/html;charset=UTF-8")
    public String addOption(@RequestParam(value = "optionQuestion",required = false) String optionQuestion,
                            @RequestParam(value = "optionA",required = false) String optionA,
                            @RequestParam(value = "optionB",required = false) String optionB,
                            @RequestParam(value = "optionC",required = false) String optionC,
                            @RequestParam(value = "optionD",required = false) String optionD,
                            @RequestParam(value = "knowledge",required = false) String knowledge,
                            @RequestParam(value = "answerContent",required = false) String answerContent,
                            @RequestParam(value = "answerType",required = false) int answerType,
                            @RequestParam(value = "answerKeys",required = false) String answerKeys) {
//        初始化变量
        Option option = new Option();
        option.setOptionQuestion(optionQuestion);
        option.setOptionA(optionA);
        option.setOptionB(optionB);
        option.setOptionC(optionC);
        option.setOptionD(optionD);
        option.setKnowledge(knowledge);

        //调用业务逻辑
        int result = optionService.addOption(option);

        result = answerService.addAnswer(answerContent,answerType,option.getOptionId(),answerKeys);

        if(result == 1) {
            CorrectJsonObject correctJsonObject = new CorrectJsonObject("操作成功");
            return JSONObject.toJSONString(correctJsonObject);
        }else {
            ErrorJsonObject errorJsonObject = new ErrorJsonObject(101,"操作失败");
            return JSONObject.toJSONString(errorJsonObject);
        }
    }

    /**
     * 插入填空题极其答案的信息
     * @param blankQuestion
     * @param knowledge
     * @param answerContent
     * @param answerType
     * @param answerKeys
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/insertBlank",method = RequestMethod.POST,produces = "text/html;charset=UTF-8")
    public String addBlank(@RequestParam(value = "blankQuestion",required = false) String blankQuestion,
                           @RequestParam(value = "knowledge",required = false) String knowledge,
                           @RequestParam(value = "answerContent",required = false) String answerContent,
                           @RequestParam(value = "answerType",required = false) int answerType,
                           @RequestParam(value = "answerKeys",required = false) String answerKeys) {

        Blank blank = new Blank();
        blank.setBlankQuestion(blankQuestion);
        blank.setKnowledge(knowledge);
        //插入填空题基本信息
        int result = blankService.addQuestion(blank);
        //插入答案
        result = answerService.addAnswer(answerContent,1,blank.getBlankId(),answerKeys);

        if(result != 0) {
            CorrectJsonObject correctJsonObject = new CorrectJsonObject("插入成功");
            return JSONObject.toJSONString(correctJsonObject);
        }else {
            ErrorJsonObject errorJsonObject = new ErrorJsonObject(101,"插入失败");
            return JSONObject.toJSONString(errorJsonObject);
        }
    }

    /**
     * 查询单选题并返回视图
     * @param request
     * @return
     */
    @RequestMapping(value = "/showOptions",method = RequestMethod.GET,produces = "text/html;charset=UTF-8")
    public String showOptions(HttpServletRequest request) {

        List<Option> optionList = optionService.showOption();

        request.setAttribute("optionList",optionList);

        return "admin/option_list";
    }

    /**
     * 查询多选题
     * @param request
     * @return
     */
    @RequestMapping(value = "/showBlanks",method = RequestMethod.GET,produces = "text/html;charset=UTF-8")
    public String showBlanks(HttpServletRequest request) {
        List<Blank> blankList = blankService.showBlank();

        request.setAttribute("blankList",blankList);

        return "admin/blank_list";
    }

    /**
     * 删除一道选择题
     * @param optionId
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/deleteAOption",method = RequestMethod.POST,produces = "text/html;charset=UTF-8")
    public String deleteARecord(@RequestParam(value = "optionId",required = false) Integer optionId) {

        //删除选择题的信息以及题干信息
        int result = optionService.deleteOption(optionId);
        result = answerService.deleteAnswer(optionId,0);

        if(result != 0) {
            CorrectJsonObject correctJsonObject = new CorrectJsonObject("删除成功");
            return JSONObject.toJSONString(correctJsonObject);
        }else {
            ErrorJsonObject errorJsonObject = new ErrorJsonObject(106,"删除失败");
            return JSONObject.toJSONString(errorJsonObject);
        }
    }

    /**
     * 删除一道填空题信息
     * @param blankId
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/deleteABlank",method = RequestMethod.POST,produces = "text/html;charset=UTF-8")
    public String deleteABlank(@RequestParam(value = "blankId",required = false) Integer blankId) {
        //删除填空题的信息
        int result = blankService.deleteABlank(blankId);

        //删除答案的信息
        result = answerService.deleteAnswer(blankId,1);

        if(result != 0) {
            CorrectJsonObject correctJsonObject = new CorrectJsonObject("删除成功");
            return JSONObject.toJSONString(correctJsonObject);
        }else {
            ErrorJsonObject errorJsonObject = new ErrorJsonObject(106,"删除失败");
            return JSONObject.toJSONString(errorJsonObject);
        }
    }
}
